//
//  ZCBaseViewController.m
//  ZCLive
//
//  Created by 彭军 on 2017/11/12.
//  Copyright © 2017年 Elvis. All rights reserved.
//

#import "ZCBaseViewController.h"
//导航栏标题文字大小
#define TITLEFONT 17
@interface ZCBaseViewController ()
@property (nonatomic, strong) NSString *titleText;
@end

@implementation ZCBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.hidesBottomBarWhenPushed = YES;
        _enableBackGesture = YES;
    }
    return self;
}

- (void)setEnableBackGesture:(BOOL)enableBackGesture {
    _enableBackGesture = enableBackGesture;
    for (UIGestureRecognizer *gesture in self.navigationController.view.gestureRecognizers) {
        gesture.enabled = enableBackGesture;
    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    for (UIGestureRecognizer *gesture in self.navigationController.view.gestureRecognizers) {
        gesture.enabled = _enableBackGesture;
    }
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = [self hiddenNavigationBar];
    [[UIApplication sharedApplication] setStatusBarStyle:[self statusBarStyle]];
}

- (void)createRightBarButtonItemWithTarget:(id)target action:(SEL)action normalImage:(UIImage *)nImage highlightedImage:(UIImage *)hImage {
    UIButton *rButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rButton.exclusiveTouch = YES;
    CGSize defaultSize = CGSizeMake(30, 44);
    if (nImage.size.width > defaultSize.width) {
        defaultSize.width = nImage.size.width;
    }
    if (hImage.size.width > defaultSize.width) {
        defaultSize.width = hImage.size.width;
    }
    rButton.frame = CGRectMake(0, 0, defaultSize.width, defaultSize.height);
    [rButton setImage:nImage forState:UIControlStateNormal];
    [rButton setImage:hImage forState:UIControlStateHighlighted];
    [rButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rButton];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = -5;
    
    self.navigationItem.rightBarButtonItems =  @[flexSpacer,rightItem];
}

- (void)createRightBarButtonItemWithTarget:(id)target action:(SEL)action title:(NSString *)title {
    CGSize defaultSize = CGSizeMake(40, 44);
    UIFont *font = [UIFont systemFontOfSize:15];
    CGSize titleSize = [title sizeWithStringFont:font constrainedToSize:CGSizeMake(MAXFLOAT, 44)];
    titleSize.width = titleSize.width < defaultSize.width ? defaultSize.width : titleSize.width + 10;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, titleSize.width, titleSize.height);
    button.titleLabel.font = font;
    [button setTitleColor:[self rightItemColor] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = -10;
    
    self.navigationItem.rightBarButtonItems =   @[flexSpacer,rightItem];
}

- (UIBarButtonItem *)createFlexSpacerBarButtonItemWithTarget:(id)target
                                                      action:(SEL)action
                                                       width:(CGFloat)width {
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                               target:target
                                                                               action:action];
    flexSpacer.width = width;
    return flexSpacer;
}

- (NSString *)backItemIcon{
    return @"back";
}

- (UIColor *)backItemColor {
    return [UIColor whiteColor];
}

- (UIColor *)rightItemColor {
    return NLColorFromRGB(0x333333);
}

- (void)setTitle:(NSString *)title {
    self.titleText = title;
    UIFont *font = [UIFont systemFontOfSize:18];
    CGSize titleSize = [title sizeWithStringFont:font constrainedToSize:CGSizeMake(200, 44)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleSize.width, titleSize.height)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = font;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [self titleColor];
    titleLabel.text = title;
    self.navigationItem.titleView = titleLabel;
}

- (NSString *)title {
    return _titleText;
}

- (UIColor *)titleColor {
    return NLColorFromRGB(0x333333);
}

- (UIStatusBarStyle)statusBarStyle {
    return UIStatusBarStyleDefault;
}



//-(UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (BOOL)hiddenNavigationBar {
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
